<?php
namespace app\admin\model;
use think\Model;

use think\facade\Db;
class Admin extends Model
{
    protected $pk = 'admin_id';
    public function login($data,$code){
        if($code=='open'){
            if(!isset($data['vercode']) || !$this->check($data['vercode'])){
                return ['code' => 0, 'msg' => '验证码错误'];
            }
        }
        $user=Db::name('admin')->where('username',$data['username'])->find();
        if($user) {
            if($user['is_open']<1){
                return ['code' => 0, 'msg' => '当前用户已禁用，请联系网站管理员!'];
            }
            if ($user['is_open']==1 && $user['pwd'] == md5($data['password'].'QQ1147361223')){
                unset($user['pwd']);
                session('admin',$user);
                return ['code' => 1, 'msg' => '登录成功!']; //信息正确
            }else{
                return ['code' => 0, 'msg' => '用户名或者密码错误，重新输入!']; //密码错误
            }
        }else{
            return ['code' => 0, 'msg' => '用户不存在!']; //用户不存在
        }
    }
    public function getInfo($admin_id){
        $info = Db::name('admin')->withoutField('pwd')->find($admin_id);
        return $info;
    }
    public function check($code){
        return captcha_check($code);
    }
    //    获取下级所有用户信息
    public function getChildrenAdminName($admin,$withself = false){
//取出所有用户组
        $authGroup = \think\facade\Db::name('auth_group')->field('id,pid,title')->order('pid asc,id asc')->select()->toArray();
//        回去当前组的所有下级
       $tree=new \fast\Tree();
       $tree->init($authGroup);
        $childrenGroupIds=$tree->getChildrenIds($admin['group_id'],false);
        if(!$childrenGroupIds) return [];
        $adminList=\think\facade\Db::name('admin')->where('group_id','in',$childrenGroupIds)->column('username');
        if(!$adminList)$adminList=[];
        if($withself)$adminList[]=$admin['username'];
        return  $adminList;

    }

    /**
     * getCustomerAdminId
     * 获取分配客资的当前管理员
     *
     * @access public
     * @author liuqi
     * @return void
     */
    public static function getCustomerAdminId() {
        $prev_id = cache('getCustomerAdminId') ?: 0;
        $admin_list = Db::name('admin')->where('standing', 'like', "%business%")->field('admin_id, username')->select();
        $admin_list = $admin_list ? $admin_list->toArray() : [];
        $admin_id = 0;
        $admin_name = 0;
        foreach ($admin_list as $v){
            if($v['admin_id'] > $prev_id) {
                $admin_id = $v['admin_id'];
                $admin_name = $v['username'];
                break;
            }
        }
        if($admin_id === 0) {
            $admin = reset($admin_list);
            $admin_id = $admin['admin_id'];
            $admin_name = $admin['username'];
        }
        cache('getCustomerAdminId', $admin_id, 84600*30);
        return $admin_name;
    }
    
    /**
     * # 根据最后轮询时间获取商务
     */
    public function getPollCustomerAdminId($last_admin="")
    {
        # 初始化返回值
        $return_data = [
            'status'=>true,
            'data'=>[],
            'msg'=>"",
        ];
//        # 获取锁定时间
//        $lock_time = 3;
//        # 获取当前时间
//        $now_time = time();
//        # 获取查询时间 (三秒内已经被轮询的商务不在参与轮询)
//        $select_time = $now_time - $lock_time;
        # 获取对应商务
        $admin = $this
            ->field('admin_id,username')
            ->where('is_poll','=',1)
//            ->where('last_poll_time','<',$select_time)
            ->where('standing','like',"%business%")
            ->when($last_admin, function ($query) use ($last_admin) {
                if(!empty($last_admin)){
                    $query->where('username','<>',$last_admin);
                }
            })
            ->order('last_poll_time ASC')
            ->find();
        if(!$admin){
            $return_data['status'] = false;
            $return_data['msg'] = "未查询到可匹配商务,请稍后重试";
        }
        $return_data['data'] = $return_data['status'] ? $admin : $return_data['data'];
        return $return_data;
    }


}

